package com.example.rbacmanage.service.app.impl;

import com.example.rbacmanage.annotation.PermissionRange;
import com.example.rbacmanage.entity.app.*;
import com.example.rbacmanage.mapper.app.*;
import com.example.rbacmanage.service.app.AppMenuService;
import com.example.rbacmanage.service.app.MenuOrderService;
import com.example.rbacmanage.util.OrderNumberUtil;
import com.github.houbb.heaven.reflect.api.IField;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MenuOrderServiceImpl implements MenuOrderService {
    @Autowired
    private MenuOrderMapper menuOrderMapper;
    @Autowired
    private UserActivityMapper userActivityMapper;
    @Autowired
    private PointsMapper pointsMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    @Override
    public void save(MenuOrder menuOrder) {
        menuOrder.setOrderNumber(OrderNumberUtil.generateOrderNumber());
        menuOrder.setCreateTime(new Date());
        menuOrderMapper.insert(menuOrder);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            menuOrderMapper.del(id);
        }
    }

    @Override
    public void update(MenuOrder menuOrder) {
        menuOrderMapper.update(menuOrder);
        if (menuOrder.getOrderStatus() != null && menuOrder.getOrderStatus().equals("1")) {
            //添加活动使用记录
            if (menuOrder.getActivityIds() != null && !menuOrder.getActivityIds().isEmpty()) {
                for (Long id : menuOrder.getActivityIds()) {
                    UserActivity userActivity = new UserActivity();
                    userActivity.setActivityId(id);
                    userActivity.setUserId(menuOrder.getUserId());
                    userActivityMapper.insert(userActivity);
                }
            }
            //积分消耗
            if (menuOrder.getPointsNum() != null && menuOrder.getPointsNum() > 0){
                Points points = new Points();
                points.setPointsUnitPrice(menuOrder.getPointsUnitPrice());
                points.setPointsNum(menuOrder.getPointsNum());
                points.setOperType("0");
                points.setOrderId(menuOrder.getId());
                pointsMapper.insert(points);

                AppUser appUser = appUserMapper.queryOneById(menuOrder.getUserId());
                if (appUser != null) {
                    if (appUser.getPoints() == null) {
                        appUser.setPoints(0);
                    }
                    if (appUser.getPoints() > 0 && appUser.getPoints() >= points.getPointsNum()) {
                        appUser.setPoints(appUser.getPoints() - points.getPointsNum());
                    }
                    appUserMapper.update(appUser);
                }

            }
            //积分新增
            Activity activity = new Activity();
            activity.setType("1");
            List<Activity> activities = activityMapper.list(activity);
            if (!activities.isEmpty()) {
                Points points = new Points();
                points.setPointsNum(activities.get(0).getOrderAddPoints());
                points.setOperType("1");
                points.setOrderId(menuOrder.getId());
                pointsMapper.insert(points);

                AppUser appUser = appUserMapper.queryOneById(menuOrder.getUserId());
                if (appUser != null) {
                    if (appUser.getPoints() == null) {
                        appUser.setPoints(0);
                    }
                    appUser.setPoints(appUser.getPoints() + points.getPointsNum());
                    appUserMapper.update(appUser);
                }
            }
        }
    }

    @Override
    public List<MenuOrder> getAll(MenuOrder menuOrder) {
        return menuOrderMapper.list(menuOrder);
    }

    @PermissionRange(shopAlias = "s")
    @Override
    public PageInfo<MenuOrder> getPage(MenuOrder menuOrder) {
        PageHelper.startPage(menuOrder.getPage(),menuOrder.getLimit());
        return new PageInfo<>(getAll(menuOrder));
    }

    @Override
    public MenuOrder getById(Long id) {
        return menuOrderMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(MenuOrder menuOrder) {
        return menuOrderMapper.count(menuOrder);
    }

    @Override
    public PageInfo<MenuOrder> page(MenuOrder menuOrder) {
        PageHelper.startPage(menuOrder.getPage(),menuOrder.getLimit());
        return new PageInfo<>(getAll(menuOrder));
    }
}
